﻿using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

namespace SumThenMod
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Instance = this;
            com = new SuperSerialPort();
            com.DataReceived = DataProcess;
        }
        SuperSerialPort com;
        public static MainWindow Instance;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (open.Content.ToString().Equals("打开"))
                {
                    com.SetPortInfo(ports.SelectedItem.ToString(), check.SelectedItem.ToString(), stop.SelectedItem.ToString(), data.SelectionBoxItem.ToString(), baud.SelectionBoxItem.ToString());
                    com.Open();
                    open.Content = "关闭";
                }
                else
                {
                    com.Close();
                    open.Content = "打开";
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ports.ItemsSource = com.GetPortNames();
            ports.SelectedIndex = 0;
            stop.ItemsSource = com.GetStopBits();
            stop.SelectedIndex = 1;
            check.ItemsSource = com.GetParitys();
            check.SelectedIndex = 0;
        }

        private void Go_Click(object sender, RoutedEventArgs e)
        {
            string src = GetSrc();
            if (src.Trim().Length > 0)
            {
                byte[] db = Util.GetSumThenMod256(src, int.Parse(startIndex.Text.Trim()));
                if (db.Length > 0)
                    com.Send(db);
            }
        }

        private string GetSrc()
        {
            TextRange textRange = new TextRange(send.Document.ContentStart, send.Document.ContentEnd);
            return textRange.Text.Trim().TrimEnd(Environment.NewLine.ToCharArray());
        }


        private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point pp = Mouse.GetPosition(this);
                //限值标题栏才可以按住拖动窗体
                if (pp.X >= 0 && pp.X <= 895 && pp.Y >= 0 && pp.Y <= 35)
                    DragMove();
            }
        }

        private void DataProcess(byte[] buf)
        {
            updateRecv(buf);
        }

        public void updateSend(byte[] db)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                recv.AppendText($"{DateTime.Now.ToLongTimeString()} >>  发送: {Util.ByteToHexString(db)}\r\n\r\n");
                recv.ScrollToEnd();
            }));
        }
        public void updateRecv(byte[] db)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                recv.AppendText($"{DateTime.Now.ToLongTimeString()} <<  接收: {Util.ByteToHexString(db)}\r\n\r\n");
                recv.ScrollToEnd();
            }));
        }

        private void Minimize_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
    }
}
